<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">一体杆名称：</span>
      <el-input
        v-model="params.poleName"
        clearable
        placeholder="请输入内容"
        class="search-main"
        @clear="getList"
      />
      <span class="search-label">一体杆编号：</span>
      <el-input
        v-model="params.poleNumber"
        clearable
        placeholder="请输入内容"
        class="search-main"
        @clear="getList"
      />
      <span class="search-label">处置状态：</span>
      <el-select v-model="params.handleStatus">
        <!-- 此处value值可传字符串01也可以传布尔值01，带冒号表示布尔 -->
        <el-option label="全部" :value="null" />

        <el-option label="未派单" :value="0" />
        <el-option label="已派单" :value="1" />
        <el-option label="已接单" :value="2" />
        <el-option label="已完成" :value="3" />
      </el-select>
      <el-button type="primary" class="search-btn" @click="getList"
        >查询</el-button
      >
    </div>

    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="list" style="width: 100%">
        <el-table-column type="index" label="序号" />
        <el-table-column label="一体杆名称" prop="poleName" />
        <el-table-column label="一体杆编号" prop="poleNumber" />
        <el-table-column label="故障类型" prop="errorType" />
        <!-- <el-table-column label="处置状态" prop="handleStatus" >
          <template #default="scope">
            {{ formatStatus(scope.row.handleStatus) }}
          </template>
        </el-table-column> -->
        <el-table-column label="处置状态" prop="handleStatus" :formatter="fn" />
        <el-table-column label="告警时间" prop="warningTime" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <!-- 当前数据的id，scope是个对象 scpoe.row.id表示这一行数据的id -->
            <el-button
              size="mini"
              type="text"
              @click="warnningDispatch(scope.row)"
              disabled="flag"
              >派单</el-button
            >
            <el-button size="mini" type="text" @click="detail(scope.row.id)"
              >详情</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="delWarnningRecord(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total,sizes, prev, pager, next, jumper"
        :total="total"
        :current-page="params.page"
        :page-sizes="[5, 10, 15, 20, 30]"
        :page-size="params.pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- //告警派单 -->

    <el-dialog
      :title="Form.id ? '告警派单' : '添加楼宇'"
      :visible="dialogVisible"
      width="580px"
      @close="closeWarn"
    >
      <div class="form-container">
        <el-form ref="hhh" :model="Form" :rules="addFormRules">
          <el-form-item label="处理人" prop="handleUser">
            <el-input v-model="Form.handleUser" />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>

        <el-button size="mini" type="primary" @click="submit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  delWarnningListAPI,
  getWarnningListAPI,
  dispatchWarnningListAPI,
} from "@/api/warnning";

export default {
  name: "WarnningRecord",
  data() {
    return {
      //提供列表数据
      list: [], //获取后端接口中的数据
      total: 0, //分页总数
      Form: { handleUser: "", userId: "" },
      addFormRules: {
        handleUser: [
          { required: true, message: "请选择处理人", trigger: "blur" },
        ],
      },
      params: {
        page: 1,
        pageSize: 10,
        poleName: "",
        poleNumber: "",
        handleStatus: "",
      },
      flag: false,
    };
  },
  methods: {
    fn(row) {
      if (row.handleStatus === 0) {
        this.flag = false;
        return "未派单";
      } else if (row.handleStatus === 1) {
        this.flag = true;
        return "已派单";
      } else if (row.handleStatus === 2) {
        this.flag = true;
        return "已接单";
      } else if (row.handleStatus === 3) {
        this.flag = true;
        return "已完成";
      }
    },
    async getList() {
      const res = await getWarnningListAPI(this.params);
      this.list = res.data.rows;
      this.total = res.data.total;
    },

    handleSizeChange(pageSize) {
      // console.log("页条数变化");
      this.params.pageSize = pageSize;
      this.getList();
    },
    handleCurrentChange(page) {
      this.params.page = page;
      this.getList();
    },

    detail(id) {
      this.$router.push("/warnDetail?id=" + id);
    },

    //删除告警记录
    // 删除逻辑
    delWarnningRecord(id) {
      if (id !== 4 || id !== 1 || id !== 2 || id !== 3) {
        this.$confirm("此操作将永久删除该告警记录, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          this.$message.error("演示数据不允许操作！");
        });
      } else {
        this.$confirm("此操作将永久删除该告警记录, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(async () => {
            await delWarnningListAPI(id);
            this.getList();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          })
          .catch((res) => {
            this.$message.error(res.data.error);
            // console.log(error);
          });
      }
    },
    // warnningDispatch(id) {
    //   dispatchWarnningListAPI().then(() => {});
    // },
    warnningDispatch(row) {
      this.dialogVisible = true;
      const { id, handleUser } = row;
      this.Form = {
        id,
        handleUser,
      };
    },
    submit() {
      if (this.$route.query.id) {
        dispatchWarnningListAPI(this.Form)
          .then(() => {
            this.$message.success("派单成功");
            this.$router.back();
          })
          .catch((error) => {
            this.$message.error(error.response.data.msg);
          });
      } else {
        this.$message.error("未捕获到id");
      }
    },
    formatStatus(status) {
      const statusMap = {
        0: "未派单",
        1: "已派单",
        2: "已接单",
        3: "已完成",
      };
      return statusMap[status];
    },

    Dispatch() {
      this.dialogVisible = true;
    },
    closeWarn() {
      this.dialogVisible = false;

      this.Form = {
        handleUser: "",
      };
    },
    disable() {
      this.isDisable = false;
    },
  },

  mounted() {
    if (this.id) {
      this.getDetail();
    }
    this.getList();
  },
};
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn {
    margin-left: 20px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}
</style>
